package com.woniuxy.service.impl;

import com.woniuxy.entity.Menus;
import com.woniuxy.mapper.MenusMapper;
import com.woniuxy.service.MenuService;
import com.woniuxy.util.ResponseData;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ：CuiYC
 * @description：
 * @date ：Created in 2025/3/26 19:11
 * @modified By：
 */
@Service
@AllArgsConstructor
public class MenuServiceImpl implements MenuService {
    @Resource
    private MenusMapper menusMapper;
    @Override
    public ResponseData<?> getUserMenuList(Integer userId) {
        /**
         * 获取用户菜单列表
         *      获取当前菜单的子菜单
         */
        List<Menus> menus = menusMapper.selectAllMenus(userId);
        List<Menus> result = getChildrenMenus(menus, 0);
        return ResponseData.ok(result);
    }
    /**
     * 获取当前菜单的子菜单
     */
    private List<Menus> getChildrenMenus(List<Menus> menus, Integer menuId) {
        List<Menus> result = new ArrayList<>();
        for (Menus menu : menus) {
            // 如果当前菜单的父ID等于传入的menuId，则递归查找其子菜单
            if (menu.getParentId().equals(menuId)) {
                // 递归调用，查找当前菜单的所有子菜单
                List<Menus> children = getChildrenMenus(menus, menu.getMenuId());
                menu.setChildrenMenus(children);
                result.add(menu);
            }
        }
//        System.out.println(result);
        return result;
    }
}
